/**
 * 
 */
var flowAChart = null;
var waveHightAChart = null;
var waveCycleAChart = null;
var waveHightChart = null;
var speedChart = null;


$('document').ready(function(){
	debugger;
	initFlowSelector();
	initWaveHightSelector();
	initSpeedSelector();
	debugger;
	flowAChart = echarts.init(document.getElementById('flowAChart'));
	waveHightAChart = echarts.init(document.getElementById('waveHightAChart'));
	waveCycleAChart = echarts.init(document.getElementById('waveCycleAChart'));
	waveHightChart = echarts.init(document.getElementById('waveHightChart'));
	speedChart = echarts.init(document.getElementById('speedChart'));
});	


$('#submit').click(function(){

	var currentTime = new Date();
	console.log(currentTime.getTime());

	if (HANGYUN.DEMO) {
		drawAverageDemo(currentTime);
		drawWaveHightChartDemo(currentTime);
		drawSpeedChartDemo(currentTime);
	} else {
		drawFlowAverageChart(currentTime);
		//TODO draw T1 wavehight average chart
		//TODO draw T1 wave circle average chart
		drawWaveHightChart(currentTime);
		drawSpeedChart(currentTime);
	}
});

/**
 * 
 * @param currentTime
 * @returns
 */
function drawFlowAverageChart(currentTime) {
	
	let startTime = new Date(currentTime);
	var t0 = $('#t0').val();
	var flowTran = $('#flowSelector').val();
	
	// get control data
	$.ajax({
		url: 'environment/flowControl.do',
		type: 'get',
		data: {
			current: startTime
		}

	}).done(function(data, status, xhr){
		let controlStartTime = null;
		let controlEndTime = null;
		let controlId = null;
		if (data != null) {
			controlStartTime = new Date(data.flowControlStime);
			controlEndTime = new Date(data.flowControlEtime);
			controlId = data.flowControlId;
		}
		let flowAveInterval = setInterval(function(){

			// get real data to draw chart
			$.ajax({
				url: 'environment/flowAverage.do',
				type: 'post',
				data: {
					start: startTime,
					interval: Number(t0),
					controlId: controlId,
					tran: flowTran
				}
			}).done((data, status, xhr)=>{

				if (data != null && data != "") {
					debugger;
					console.log(data);
					var step = 0;
					for (var i = 0; i <= 5; i++) {
						optionFlowAverage.xAxis.data[i] = step;
						step = step + Number(t0);
					}
					
					for (var i = 4; i > 0; i--) {
						optionFlowAverage.series[0].data[i] = optionFlowAverage.series[0].data[i-1];
					}
					optionFlowAverage.series[0].data[0] = data;
					flowAChart.setOption(optionFlowAverage);
				} else {
					clearInterval(flowAveInterval);
				}
				
				if (startTime > controlEndTime) {
					clearInterval(flowAveInterval);
				}
				
			});

		}, Number(t0)*1000);
	});
}

/**
 * 
 * @param currentTime
 * @returns
 */
function drawWaveHightChart(currentTime) {
	
	let startTime = new Date(currentTime);
	var tran = $('#waveHightSelector').val();
	
	// get control data
	$.ajax({
		url: 'environment/waveHightControl.do',
		type: 'get',
		data: {
			current: startTime
		}

	}).done(function(data, status, xhr){
		let controlStartTime = null;
		let controlEndTime = null;
		let controlId = null;
		if (data != null) {
			controlStartTime = new Date(data.waveheightControlStime);
			controlEndTime = new Date(data.waveheightControlEtime);
			controlId = data.waveheightControlId;
		}

		let waveHightInterval = setInterval(function(){

			// get real data to draw chart
			$.ajax({
				url: 'environment/waveHightData.do',
				type: 'post',
				data: {
					start: startTime,
					interval: HANGYUN.REFRESH_INTERVAL,
					controlId: controlId,
					tran: tran
				}
			}).done((data, status, xhr)=>{

				if (data != null && data.length > 0) {
					console.log(data);
					optionWavehight.series[0].data = data;
					waveHightChart.setOption(optionWavehight);
				} else {
					clearInterval(waveHightInterval);
				}

				if (startTime > controlEndTime) {
					clearInterval(waveHightInterval);
				}

			});
		}, HANGYUN.REFRESH_INTERVAL*1000);

	});
}

/**
 * 
 * @param currentTime
 * @returns
 */
function drawSpeedChart(currentTime) {
	
	let startTime = new Date(currentTime);
	var tran = $('#speedSelector').val();
	
	// get control data
	$.ajax({
		url: 'environment/speedControl.do',
		type: 'get',
		data: {
			current: startTime
		}

	}).done(function(data, status, xhr){
		let controlStartTime = null;
		let controlEndTime = null;
		let controlId = null;
		if (data != null) {
			controlStartTime = new Date(data.currentspeedControlStime);
			controlEndTime = new Date(data.currentspeedControlEtime);
			controlId = data.currentspeedControlId;
		}

		let speedInterval = setInterval(function(){

			// get real data to draw chart
			$.ajax({
				url: 'environment/speedData.do',
				type: 'post',
				data: {
					start: startTime,
					interval: HANGYUN.REFRESH_INTERVAL,
					controlId: controlId,
					tran: tran
				}
			}).done((data, status, xhr)=>{
				if (data != null && data.length > 0) {
					console.log(data);
					optionSpeed.series[0].data = data;
					speedChart.setOption(optionSpeed);
				} else {
					clearInterval(speedInterval);
				}

				if (startTime > controlEndTime) {
					clearInterval(speedInterval);
				}

			});

		}, HANGYUN.REFRESH_INTERVAL*1000);
		
	});
}

/**
 * 
 * @returns
 */
function initFlowSelector(){
	
	$.ajax({
		url: 'environment/flowTrans.do',
        method : "get",
	}).done(function(data, status, xhr){
		for (let i = 0; i < data.length; i++){
			$('#flowSelector').append(
			'<option value='+data[i].transducerNumber+'>'+data[i].transducerNumber+'</option>');
		}
	});
}

/**
 * 
 * @returns
 */
function initWaveHightSelector(){
	
	$.ajax({
		url: 'environment/waveHightTrans.do',
        method : "get",
	}).done(function(data, status, xhr){
		for (let i = 0; i < data.length; i++){
			$('#waveHightSelector').append(
			'<option value='+data[i].transducerNumber+'>'+data[i].transducerNumber+'</option>');
		}
	});
}

/**
 * 
 * @returns
 */
function initSpeedSelector(){
	
	$.ajax({
		url: 'environment/speedTrans.do',
        method : "get",
	}).done(function(data, status, xhr){
		for (let i = 0; i < data.length; i++){
			$('#speedSelector').append(
			'<option value='+data[i].transducerNumber+'>'+data[i].transducerNumber+'</option>');
		}
	});
}


/**
 * demo when no data
 * @param currentTime
 * @returns
 */
function drawWaveHightChartDemo (currentTime) {
	
	let startTime = new Date(currentTime);
	let endTime = new Date(startTime.getTime()+HANGYUN.DEMO_DURATION*1000*60);
	
	let inter = setInterval(()=>{
		let valueArray = [];
		let num = 0;
		for (let i = 0; i < 100; i++) {
			num = Math.random()*(5-3) + 3;
			valueArray.push(num);
		}
		
		optionWavehight.series[0].data = valueArray;
		waveHightChart.setOption(optionWavehight);
		
		startTime.setSeconds(startTime.getSeconds() + HANGYUN.REFRESH_INTERVAL);
		
		if (startTime > endTime) {
			clearInterval(inter);
		}
		
	}, HANGYUN.REFRESH_INTERVAL*1000);
}

/**
 * demo when no data
 * @param currentTime
 * @returns
 */
function drawSpeedChartDemo(currentTime) {
	
	let startTime = new Date(currentTime);
	let endTime = new Date(startTime.getTime()+HANGYUN.DEMO_DURATION*1000*60);
	
	let inter = setInterval(()=>{
		let valueArray = [];
		let num = 0;
		for (let i = 0; i < 100; i++) {
			num = Math.random()*(5-3) + 3;
			valueArray.push(num);
		}
		
		optionSpeed.series[0].data = valueArray;
		speedChart.setOption(optionSpeed);
		
		startTime.setSeconds(startTime.getSeconds() + HANGYUN.REFRESH_INTERVAL);
		
		if (startTime > endTime) {
			clearInterval(inter);
		}
		
	}, HANGYUN.REFRESH_INTERVAL*1000);
}


/**
 * demo when no data
 * @param currentTime
 * @returns
 */
function drawAverageDemo(currentTime) {
	
	var t0 = $('#t0').val();
	let startTime = new Date(currentTime);
	let endTime = new Date(startTime.getTime()+HANGYUN.DEMO_DURATION*1000*60);
	
	let inter = setInterval(()=>{
		let valueArray = [];
		let num = 0;
		num = Math.random().toFixed(2)*(5-3) + 3;
		var step = 0;
		for (var i = 0; i <= 5; i++) {
			optionFlowAverage.xAxis.data[i] = step;
			step = step + Number(t0);
		}
		
		for (var i = 4; i > 0; i--) {
			optionFlowAverage.series[0].data[i] = optionFlowAverage.series[0].data[i-1];
		}
		optionFlowAverage.series[0].data[0] = num;
		flowAChart.setOption(optionFlowAverage);
		waveHightAChart.setOption(optionFlowAverage);
		waveCycleAChart.setOption(optionFlowAverage);
		
		startTime.setSeconds(startTime.getSeconds() + HANGYUN.REFRESH_INTERVAL);
		
		if (startTime > endTime) {
			clearInterval(inter);
		}
		
	}, HANGYUN.REFRESH_INTERVAL*1000);
}


